package com.ziq.zcamp.post

import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.ziq.base.extension.BindingViewHolder
import java.util.*

abstract class RecyclerViewDragAdapter<T : BindingViewHolder<F>, E, F : ViewBinding>(var list: MutableList<E>) : RecyclerView.Adapter<T>() {

    override fun getItemCount(): Int {
        return list.size
    }

    fun setDate(data: MutableList<E>){
        list = data
        notifyDataSetChanged()
    }

    fun getData() : MutableList<E>{
        return list
    }

    open fun canDrag(position : Int) : Boolean{
        return true
    }

    open fun canSwap(fromPosition: Int, toPosition: Int) : Boolean{
        return true
    }

    fun swap(fromPosition: Int, toPosition: Int){
        Collections.swap(list, fromPosition, toPosition)
        notifyItemMoved(fromPosition, toPosition)
    }

    fun remove(delPos : Int){
        list.removeAt(delPos)
        notifyItemRemoved(delPos)
        //notifyItemRangeChanged(delPos, list.size - delPos)
    }
}